//app.js
var plublic = require('/common/plublic.js');
var PostData = require('/common/PostData.js');
import {net, Connect} from '/utils/net'

App({
    onLaunch: function () {
        /**
         * @author 焦政
         * @date 2019/7/3
         * @Description: 测试存储用户信息
        */
        wx.setStorageSync('customerUuid','692dcdc33153467fa75983580f0cf60e')
        wx.setStorageSync('token','692dcdc33153467fa75983580f0cf60e')
        wx.setStorageSync('sessionId','692dcdc33153467fa75983580f0cf60e')


        if (wx.canIUse('getUpdateManager')) {
            const updateManager = wx.getUpdateManager()
            updateManager.onCheckForUpdate(function (res) {
                if (res.hasUpdate) {
                    updateManager.onUpdateReady(function () {
                        wx.showModal({
                            title: '更新提示',
                            content: '新版本已经准备好，是否重启应用？',
                            success: function (res) {
                                if (res.confirm) {
                                    updateManager.applyUpdate()
                                }
                            }
                        })
                    })
                    updateManager.onUpdateFailed(function () {
                        wx.showModal({
                            title: '已经有新版本了哟~',
                            content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
                        })
                    })
                }
            })
        } else {
            wx.showModal({
                title: '提示',
                content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
            })
        }
    },
    /**
     * 获取用户信息
     */
    getNewUserinfo: function (errMsg, loginCode, encryptedData, iv, cb) {
        var that = this;

        if (errMsg == "getUserInfo:ok") {
            wx.showLoading({
                title: '登录中',
            });
            var data = {
                customerNo: that.globalData.fromCustormerNo,
                jscode: loginCode,
                encryptedData: encryptedData,
                iv: iv,
            };
            var optionss = {
                'data': data
            };
            PostData(optionss, function (res) {
                if (res.data.return_code === "0") {
                    console.log('from:' + that.globalData.fromCustormerNo)
                    console.log('my:' + res.data.data.customerNo)
                    wx.setStorageSync('token', res.data.token);
                    wx.setStorageSync('tokenApp', res.data.tokenApp);
                    wx.setStorageSync('customerUuid', res.data.data.uuid);
                    wx.setStorageSync('customerNo', res.data.data.customerNo);
                    wx.setStorageSync('sessionId', res.data.sessionId);
                    wx.setStorageSync('openid', res.data.openId);
                    wx.setStorageSync('session_key', res.data.sessionKey);
                    wx.setStorageSync('nickName', res.data.data.customerThirdMsgDTO.thirdName);


                    wx.setStorageSync('firstLogin', res.data.data.firstLogin);//是否第一次登录
                    console.log(res.data.data.firstLogin)
                    var mobile = '';
                    if (res.data.data.mobile) {
                        mobile = res.data.data.mobile;
                    }
                    wx.setStorageSync('mobile', mobile);
                    that.globalData.openid = res.data.openId;
                    that.globalData.token = res.data.token;
                    that.globalData.tokenApp = res.data.tokenApp;
                    that.globalData.sessionId = res.data.sessionId;
                    that.globalData.customerUuid = res.data.data.uuid;
                    that.globalData.session_key = res.data.sessionKey;
                    that.globalData.mobile = mobile;

                    console.log('my1:' + that.globalData.customerNo)
                    that.globalData.customerNo = res.data.data.customerNo;
                    console.log('my2:' + that.globalData.customerNo)

                    that.globalData.firstLogin = res.data.data.firstLogin;
                    cb(res);
                } else {
                    console.log(res);
                }

                wx.hideLoading();//关闭加载状态
            }, "thirdLoginAndRegister", 1)
        } else {

            wx.hideLoading();//关闭加载状态
            wx.showModal({
                title: '提示',
                content: '您点击了拒绝授权,将无法正常显示个人信息,请重新点击获取授权。',
                showCancel: false,
                success: function (res) {

                }
            })
        }
    },
    /**
     * 获取用户微信手机号码
     */
    getPhoneNumber: function (errMsg, iv, encryptedData, session_key, customerUuid, cb) {
        var that = this;
        if (errMsg === "getPhoneNumber:ok") {
            var info = {
                "iv": iv,
                "encryptedData": encryptedData,
                "sessionKey": session_key,
                "customerUuid": customerUuid,
            }
            var options = {
                'data': info
            }
            PostData(options, function (res) {
                if (res.data.return_code === "0") {
                    that.globalData.mobile = res.data.data.purePhoneNumber
                    wx.setStorageSync('mobile', res.data.data.purePhoneNumber)
                    cb(res.data.data.purePhoneNumber)
                } else {
                    if (res.data.return_code === "988") {

                        wx.showModal({
                            title: '提示',
                            content: res.data.message + ',请手动修改',
                            showCancel: false,
                            success: function (res) {

                            }
                        })
                    } else {
                        wx.showToast({
                            title: res.data.message,
                            icon: 'loading',
                            duration: 5000,
                        })
                    }
                }
            }, "getPhoneNumber", 1)
        } else {
            wx.showModal({
                title: '提示',
                content: '您点击了拒绝授权,将无法同步您的微信手机换号,请重新点击获取授权。',
                showCancel: false,
                success: function (res) {

                }
            })
        }
    },
    /**
     * 检查Sessionkey是否过期
     */
    checkSession: function (cb) {
        wx.checkSession({
            success: function () {
                cb(wx.getStorageSync('session_key'))
            },
            fail: function () {
                wx.login({
                    success: function (res) {
                        if (res.code) {
                            var info = {
                                "jscode": res.code,
                            }
                            var options = {
                                'data': info
                            }
                            PostData(options, function (res) {
                                if (res.data.return_code === "0") {
                                    that.globalData.session_key = res.data.session_key
                                    wx.setStorageSync('session_key', res.data.session_key)
                                    cb(res.data.session_key)
                                }
                            }, "tempLogin", 1)
                        }
                    }
                });
            }
        })
    },
    globalData: {
        userInfo: null,
    },
    $net: net,
    $imgURL: Connect.imgUrl

})